//
// Created by wei.yang on 2021/8/24.
//

#include "pcm_decoder.h"
#include "pcm_decoder_controller.h"

extern "C" {
/**
 * 初始化
 * @param env
 * @param obj
 * @param mediaFilePath 待解码的媒体文件路径
 * @param pcmFilePath  生成pcm文件的路径
 * @return
 */
PCMDecoderController *controller;
JNIEXPORT jint JNICALL
Java_com_yw_ffmpeg_decoder_PcmDecoder_init(JNIEnv *env, jobject obj, jstring mediaFilePath,
                                           jstring pcmFilePath) {
    //将jni字符串转换为c能识别的字符数组
    const char *pcmPath = env->GetStringUTFChars(pcmFilePath, NULL);
    const char *mediaPath = env->GetStringUTFChars(mediaFilePath, NULL);
    controller = new PCMDecoderController();
    controller->init(mediaPath, pcmPath);
    //销毁字符串
    env->ReleaseStringUTFChars(mediaFilePath, mediaPath);
    env->ReleaseStringUTFChars(pcmFilePath, pcmPath);
    return 0;
}

/**
 * 解码
 * @param env
 * @param obj
 */
JNIEXPORT void JNICALL Java_com_yw_ffmpeg_decoder_PcmDecoder_decode(JNIEnv *env, jobject obj) {
    if (controller) {
        controller->decode();
    }
}
/**
 * 销毁
 * @param env
 * @param obj
 */
JNIEXPORT void JNICALL Java_com_yw_ffmpeg_decoder_PcmDecoder_destroy(JNIEnv *env, jobject obj) {
    if (controller) {
        controller->destroy();
        delete controller;
        controller = NULL;
    }
}
}

